package com.syni.mdd.yhd.common.all.service.order.impl;

import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.constants.ResultCode;
import com.syni.mdd.yhd.common.all.entity.BmsDxOrder;
import com.syni.mdd.yhd.common.all.entity.order.OrderCommitHolder;
import com.syni.mdd.yhd.common.all.service.BmsDxOrderService;
import com.syni.mdd.yhd.common.all.service.order.OrderCommitService;
import com.syni.mdd.yhd.common.all.service.platformGoods.BmsPlatformGoodService;
import com.syni.mdd.yhd.common.all.utils.RequestParamUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.ArithUtil;
import com.syni.mdd.sjb.common.component.utils.common.utils.ParamsMap;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.persistence.EntityManager;
import java.util.Map;

/**
 * @className PlatformOrderServiceImpl
 * @description TOO
 * @Author cfx
 * @DATE 2020/3/13 22:34
 * @VERSION 1.0
 **/
@Component
public class OnlineOrderServiceImpl implements OrderCommitService {
    @Autowired
    BmsPlatformGoodService bmsPlatformGoodService;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    BmsDxOrderService bmsDxOrderService;

    @Autowired
    EntityManager entityManager;
    @Override
    public OrderCommitHolder commitLogic(OrderCommitHolder orderCommitHolder) {
        ParamsMap paramsMap = orderCommitHolder.getParamsMap();
        Map<String, Object> checkMap = RequestParamUtils.checkParams(paramsMap, "totalConsum", "actualConsum");
        if (!(checkMap.containsKey("status") && ResultCode.CODE_NORMAL.equals(checkMap.get("status").toString()))) {
            orderCommitHolder.getExecutionResult().putAll(checkMap);
            return orderCommitHolder;
        }
        Double totalConsum = Double.parseDouble(paramsMap.get("totalConsum").toString());
        Double actualConsum = totalConsum;
        if(actualConsum < 0d){
            orderCommitHolder.getExecutionResult().putAll(ResultMapHelper.result(ResultCode.CODE_ERROR_100,"买单金额必须大于0"));
            return orderCommitHolder;
        }
        Double shopConsum = ArithUtil.mul(actualConsum, Constants.SEETLE_LINES);
        Double settleMoney = ArithUtil.mul(actualConsum, Constants.SEETLE_LINES);
        BmsDxOrder bmsDxOrder = orderCommitHolder.getBmsDxOrder();
        bmsDxOrder.setTotalConsum(totalConsum);
        bmsDxOrder.setActualConsum(actualConsum);
        bmsDxOrder.setSettleMoney(settleMoney);
        bmsDxOrder.setShopConsum(shopConsum);
        orderCommitHolder.getExecutionResult().putAll(ResultMapHelper.success());
        return orderCommitHolder;
    }
}
